<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>前端定位模块</title>
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no">
    <style>
        * {
            margin: 0;
            padding: 0;
            border: 0;
        }
        body {
            position: absolute;
            width: 100%;
            height: 100%;
            text-align: center;
        }
        #pos-area {
            background-color: #009DDC;
            margin-bottom: 10px;
            width: 100%;
            overflow: scroll;
            text-align: left;
            color: white;
        }
        #demo {
            padding: 8px;
            font-size: small;
        }
        #btn-area {
            height: 100px;
        }
        button {
            margin-bottom: 10px;
            padding: 12px 8px;
            width: 42%;
            border-radius: 8px;
            background-color: #009DDC;
            color: white;
        }
    </style>
    <script type="text/javascript" src="https://3gimg.qq.com/lightmap/components/geolocation/geolocation.min.js"></script>
</head>
<body>
    <div id="name"></div>
    <div id="pos-area">
        <p id="demo">点击下面的按钮，获得对应信息：<br /></p>
    </div>

    <div id="btn-area">
        <button onClick="geo.getLocation(showPosition, showErr, options)">获取精确定位信息</button>
    </div>
    <script type="text/JavaScript">
        var geo = null;

        document.getElementById("name").innerHTML = navigator.userAgent;
        if (navigator.userAgent.search("(MQQBrowser|Edge)") !== -1) {  // 不是-1说明是 腾讯的浏览器
            geo = new qq.maps.Geolocation("Z7LBZ-AZDKG-ZMEQT-IUULT-6UBBS-XGF2Q", "softWebKey");
        } else {
            geo = navigator.geolocation;
            navigator.geolocation.getLocation = function (fun1, fun2, options) {
                navigator.geolocation.getCurrentPosition(function(p) {
                    var lng = p.coords.longitude;
                    var lat = p.coords.latitude;
                    var acy = p.coords.accuracy;
                    fun1({lng: lng, lat: lat, accuracy: acy});
                }, fun2, options);
            };
        }

        document.getElementById("pos-area").style.height = (document.body.clientHeight - 110) + 'px';

        var positionNum = 0;
        var options = {timeout: 8000};
        function showPosition(position) {
            console.log(position);
            positionNum ++;
            document.getElementById("demo").innerHTML += "序号：" + positionNum;
            document.getElementById("demo").appendChild(document.createElement('pre')).innerHTML = JSON.stringify(position, null, 4);
            document.getElementById("pos-area").scrollTop = document.getElementById("pos-area").scrollHeight;
        }

        function showErr() {
            positionNum ++;
            document.getElementById("demo").innerHTML += "序号：" + positionNum;
            document.getElementById("demo").appendChild(document.createElement('p')).innerHTML = "定位失败！";
            document.getElementById("pos-area").scrollTop = document.getElementById("pos-area").scrollHeight;
        }
    </script>
</body>
</html>
